package com.shxy.rlzy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.entity.Role;
import com.shxy.rlzy.entity.User;
import com.shxy.rlzy.entity.UserRole;
import com.shxy.rlzy.service.IRoleService;
import com.shxy.rlzy.service.IUserRoleService;
import com.shxy.rlzy.service.IUserService;
import com.shxy.rlzy.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanhao
 * @since 2021-08-16
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController extends BaseController {
    private IUserRoleService userRoleService;
    private IRoleService roleService;
    private IUserService userService;
    private JwtUtils jwtUtils;

    @Autowired
    public void setUserRoleService(IUserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }
    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }
    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setJwtUtils(JwtUtils jwtUtils) {
        this.jwtUtils = jwtUtils;
    }


    /**
     * 获取当前用户的角色
     * @return
     */
    @GetMapping("/current_role")
    public Result getCurrentRole() {
        // 获取用户名
        String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 根据用户名获取用户信息
        UserRole userRole = userRoleService.getUserRoleByName(userName);
        return Result.succ(userRole);
    }

    /**
     * 获取用户角色信息
     * @param userId
     * @return
     */
    @GetMapping("/get/{userId}")
    @PreAuthorize("hasAuthority('sys:user:role')")
    public Result getUserRole(@PathVariable Long userId) {
        UserRole userRole = userRoleService.getUserRoleByUserId(userId);
        return Result.succ(userRole);
    }

    /**
     * 设置用户角色新
     * @param userRole
     * @return
     */
    @PostMapping("/set1")
    @PreAuthorize("hasAuthority('sys:role:perm')")
    public Result save(@RequestBody UserRole userRole) {
        // 获取修改前的用户角色信息
        UserRole ur = userRoleService.getById(userRole.getId());
        if(ur != null) {
            // 如果之前用户是管理员，则不允许修改
            if(roleService.getById(ur.getRoleId()).getCode().equals("admin")) {
                return Result.fail("禁止修改管理员权限");
            }
        }
        // 修改用户角色
        if(userRoleService.saveOrUpdate(userRole)) {
            return Result.succ("设置角色成功!");
        }
        return Result.fail("设置角色失败!");
    }

    @PutMapping("/set/{userId}/{roleId}")
    public Result set(@PathVariable long userId, @PathVariable long roleId) {
        Role role = roleService.getByUserId(userId);
        Role role1 = roleService.getById(roleId);
        System.out.println(roleId);
        if(role.getCode().equalsIgnoreCase("admin") || role1.getCode().equalsIgnoreCase("admin")){
             return Result.fail("无法设置超级管理员");
        }
        UserRole userRole = userRoleService.getUserRoleByUserId(userId);
        userRole.setRoleId(roleId);
        userRoleService.saveOrUpdate(userRole);
        return Result.succ("修改角色成功");
    }

}
